/**
 * @author Helio
 */
var LedgerStatisticsView = Ext.extend(BasicPanel,{
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.GroupingStore({
			    proxy: new Ext.data.MemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'records',
					fields: ['name', {name:'amount',type:'float'}, 'type', 'date','comment','title']
			    }),
	            sortInfo: {field: 'date', direction: 'ASC'},
	            groupField: 'title'

			});
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			// define a custom summary function
		    //Ext.ux.grid.GroupSummary.Calculations['totalCost'] = function(v, record, field){
		       // return v + (record.data.estimate * record.data.rate);
		    //};

			// utilize custom extension for Hybrid Summary
		    var summary = new Ext.grid.GroupSummary();

			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				//title: '日销售状况',
				columns: [csm,{
					header: "账户名称",
					dataIndex: 'name',
					width: 100,
					summaryRenderer: function(v, params, data){
	                    return ('合计：');
	                },

					sortable: true
				}, {
					header: "金额",
					dataIndex: 'amount',
	           		align: 'right',
					width: 80,
					summaryType:'sum',
					sortable: true
				}, {
					header: "入账/出账",
					dataIndex: 'type',
					align: 'center',
					width: 50,
					summaryType:'max',
					renderer: SALE.Utils.moneyStatusRenderer.createDelegate(this)					
				}, {
	           		header: "日期",
	           		dataIndex: 'date',
	           		width: 70,
	           		summaryType:'max',
					sortable: true
	        	}, {
					header: "备注",
					dataIndex: 'comment',
					width: 80
				}, {
	           		header: "统计",
	           		dataIndex: 'title'
	        	}],
				
				sm: csm,
				
				tbar:['->', '选择查看日期：', {
					xtype:'datefield',
				    name: 'entry_date',							
					format:'Y-m-d',
					
					listeners: {
						'select': function(field, date){
							var query = Ext.util.Format.date(date,"Y-m-d");
							SALE.Action.searchLedgerStatisticsData(this.initlizeDataHandler.createDelegate(this),{'type':this.task,'date':query});
						}.createDelegate(this)
					}
				}],
				
				//bbar: pagingBar,
				
				plugins: summary,
				
				view: new Ext.grid.GroupingView({
		            forceFit:true,
		            //groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})',
		            showGroupName: false,
		            enableNoGroups:true,
					enableGroupingMenu:false,
		            hideGroupedColumn: true
		        }),
						
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'code',
				layout: 'fit'
			});
		}
		
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['records'] = resultJson.records;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.searchLedgerStatisticsData(this.initlizeDataHandler.createDelegate(this),{'type':this.task});
		}
		return this.detailsGrid;
	}	
});

LedgerStatisticsView.showNextOperatePanel = function(type){
	var  view = new LedgerStatisticsView({'task': type});
	
	if(type == 0 || type == 1) {
		var title = "入账统计";
	} else {
		var title = "出账统计";
	}
	
	SALE.App.showNextOperateTabPanel(view.getCorePanel(),title);
}